/**
 *FontPane.java
 *@anthor Daniel Liu
 *2006-5-9 ����02:50:27
 **/
package org.lex.swing.fontchooser.simple;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JPanel;

import org.lex.swing.fontchooser.FontStyle;
import org.lex.swing.fontchooser.JFontChooser;
import org.lex.swing.fontchooser.ListPane;


public class SimpleFontChooserPane extends JPanel {
	private ListPane namePane = null;
	private ListPane stylePane = null;
	private ListPane sizePane = null;

	private Font font = null;

	public SimpleFontChooserPane() {
		super();
		initialize();
		this.hookFontAttributeChangeListener();
	}

	public Font getSelectedFont() {
		return font;
	}

	public void setSelectedFont(Font font) {
		this.font = font;
		this.getNamePane().setSelectedValue(font.getFamily());
		this.getStylePane().setSelectedValue(FontStyle.parseFromFont(font).getDisplayName());
		this.getSizePane().setSelectedValue(font.getSize());
	}

	public void addFontChangeListener(PropertyChangeListener listener) {
		this.addPropertyChangeListener(JFontChooser.FONT_CHANGED_PROPERTY, listener);
	}

	private void hookFontAttributeChangeListener() {
		SimpleFontAttributeChangeHandler h = new SimpleFontAttributeChangeHandler();
		this.getNamePane().addPropertyChangeListener2(h);
		this.getStylePane().addPropertyChangeListener2(h);
		this.getSizePane().addPropertyChangeListener2(h);
	}

	/**
	 * ����GUI
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints2.gridy = 0;
		gridBagConstraints2.weightx = 0.2;
		gridBagConstraints2.weighty = 1.0;
		gridBagConstraints2.insets = new java.awt.Insets(0, 2, 2, 2);
		gridBagConstraints2.ipadx = 16;
		gridBagConstraints2.gridx = 2;
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints1.gridy = 0;
		gridBagConstraints1.weightx = 0.3;
		gridBagConstraints1.weighty = 1.0;
		gridBagConstraints1.insets = new java.awt.Insets(0, 2, 2, 3);
		gridBagConstraints1.ipadx = 20;
		gridBagConstraints1.gridx = 1;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.insets = new java.awt.Insets(0, 2, 2, 3);
		gridBagConstraints.gridx = 0;

		this.setLayout(new GridBagLayout());
		this.setSize(300, 200);
		this.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 12));
		this.add(getNamePane(), gridBagConstraints);
		this.add(getStylePane(), gridBagConstraints1);
		this.add(getSizePane(), gridBagConstraints2);
	}

	private ListPane getNamePane() {
		if (namePane == null) {
			namePane = new ListPane();
			namePane.setTitle("Name");
			Comparable[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
			namePane.setListData(fontNames);
			namePane.setPropertyName(SimpleFontAttribute.FAMILY.name());
			// namePane.getTextField().getDocument().addDocumentListener(
			// new InputCompletion( namePane.getList(), namePane.getTextField()
			// ) );
		}
		return namePane;
	}

	private ListPane getStylePane() {
		if (stylePane == null) {
			stylePane = new ListPane();
			stylePane.setTitle("Style");

			FontStyle[] all = FontStyle.values();
			Comparable[] names = new String[all.length];
			for (int i = 0; i < all.length; i++)
				names[i] = all[i].getDisplayName();
			stylePane.setListData(names);
			stylePane.setSelectedValue(FontStyle.DEFAULT_STYLE.getDisplayName());
			stylePane.setPropertyName(SimpleFontAttribute.STYLE.name());
		}
		return stylePane;
	}

	private ListPane getSizePane() {
		if (sizePane == null) {
			sizePane = new ListPane();
			sizePane.setTitle("Size");
			sizePane.setListData(8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72);
			sizePane.setSelectedValue(12);
			sizePane.setPropertyName(SimpleFontAttribute.SIZE.name());
		}
		return sizePane;
	}

	class SimpleFontAttributeChangeHandler implements PropertyChangeListener {
		private SimpleFontFactory factory = new SimpleFontFactory();
		private Font newFont = null;

		public void propertyChange(PropertyChangeEvent e) {
			String property = e.getPropertyName();
			Object newValue = e.getNewValue();
			// System.out.println( "Attribute: " + property + "\tValue= " +
			// newValue );

			SimpleFontAttribute sfa = SimpleFontAttribute.valueOf(property);
			if (sfa == SimpleFontAttribute.STYLE)
				newValue = FontStyle.parseFromDiaplayName(newValue.toString()).getStyle();
			factory.set(SimpleFontAttribute.valueOf(property), newValue);

			newFont = factory.newFont();
			Font old = font;
			font = newFont;
			firePropertyChange(JFontChooser.FONT_CHANGED_PROPERTY, old, newFont);
		}
	}
}
